package com.elgenring.org.controller;

import com.elgenring.basic.utils.AjaxResult;
import com.elgenring.basic.utils.PageList;
import com.elgenring.org.domain.Employee;
import com.elgenring.org.query.EmployeeQuery;
import com.elgenring.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 控制层
 * @Author: Atlas
 * @Version: 1.0
 * @Date: 2022/6/23 18:44
 */

@Api(value = "部门API", description = "部门表相关的CRUD功能")
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * @Title: loadById
     * @Description: 根据id查询
     * @Author: Atlas
     * @Version: 1.0
     * @Date: 2022/6/23 18:45
     * @Parameters: id
     * @Return
     */

    @ApiOperation(value = "通过id查询")
    @GetMapping("/{id}")
    @ResponseBody
    public Employee loadById(@ApiParam(value = "主键", required = true) @PathVariable("id") Long id) {

        return employeeService.loadById(id);
    }

    @ApiOperation(value = "查询所有")
    @GetMapping
    @ResponseBody
    public List<Employee> loadAll() {

        return employeeService.loadAll();
    }

    /**
     * @Title: queryData
     * @Description: 分页查询
     * @Author: Atlas
     * @Version: 1.0
     * @Date: 2022/6/23 18:45
     * @Parameters: employeeQuery
     * @Return
     */
    @ApiOperation(value = "查询所有，与内容的数量，将JS数据封装好发送给前端")
    @PostMapping
    public PageList<Employee> queryData(@ApiParam(value = "分页查询", required = true) @RequestBody EmployeeQuery employeeQuery) {


        return employeeService.queryData(employeeQuery);

    }

    /**
     * @Title: remove
     * @Description: 根据id删除
     * @Author: Atlas
     * @Version: 1.0
     * @Date: 2022/6/23 18:46
     * @Parameters: id
     * @Return
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@ApiParam(value = "主键", required = true) @PathVariable("id") Long id) {

        try {

            employeeService.remove(id);

            return AjaxResult.getAjaxResult();
        } catch (Exception e) {

            e.printStackTrace();

            return AjaxResult.getAjaxResult().setMessage("系统繁忙，请重试").setSuccess(false);
        }
    }

    /**
     * @Title:
     * @Description: 根据id批量删除
     * @Author: Atlas
     * @Version: 1.0
     * @Date: 2022/6/25 19:41
     * @Parameters: long数组
     * @Return
     */
    @ApiOperation(value = "根据id批量删除")
    @PatchMapping
    public AjaxResult batRemove(@ApiParam(value = "批量删除所选择的行", required = true) @RequestBody Long[] ids) {

        System.out.println(ids);

        try {

            employeeService.batRemove(ids);

            return AjaxResult.getAjaxResult();
        } catch (Exception e) {

            e.printStackTrace();

            return AjaxResult.getAjaxResult().setMessage("系统繁忙，请重试").setSuccess(false);
        }
    }

    /**
     * @Title: update
     * @Description: 根据id修改
     * @Author: Atlas
     * @Version: 1.0
     * @Date: 2022/6/23 18:46
     * @Parameters: employee
     * @Return
     */
    @PutMapping
    @ApiOperation(value = "新增或者根据id修改")
    public AjaxResult addOrUpt(@ApiParam(value = "部门表的所有字段", required = true) @RequestBody Employee employee) {


        try {

            if (employee.getId() == null) {

                employeeService.save(employee);


                return AjaxResult.getAjaxResult();
            } else {

                employeeService.update(employee);

                return AjaxResult.getAjaxResult();
            }
        } catch (Exception e) {
            e.printStackTrace();

            if (employee.getDept().getId() == null){

                return AjaxResult.getAjaxResult().setMessage("前填写部门").setSuccess(false);
            }

            return AjaxResult.getAjaxResult().setMessage("系统繁忙，请重试").setSuccess(false);
        }
    }

}
